package evthandler;

import filehandler.Writer;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author Casper, Dennis, Jackie, Jakob
 * @version 1.00
 */
public class EventMessage {

    private Exception ex;
    private Date date;
    private String title;
    private String msg;
    public static final DateFormat TIMEDATEFMT = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    public static final DateFormat DATEFMT = new SimpleDateFormat("ddMMyyyy");

    /**
     *
     */
    public EventMessage() {
        date = new Date();
    }

    /**
     *
     */
    public EventMessage(String title, String msg) {
        this.title = title;
        this.msg = msg;
        date = new Date();
    }

    /**
     * Shows a dialog with the event message
     * @param title
     * @param msg
     */
    public void showMessage() {
        new MessageDialog(null, true).showDialog(title, msg);
    }

    /**
     * Shows a dialog with the exception message
     * @param title
     * @param msg
     * @param ex
     */
    public void showException(Exception ex) {
        this.ex = ex;
        new MessageDialog(null, true).showDialog(title, msg + "\n The error has been logged");
        writeLogFile();
    }

    /**
     * Shows a dialog with the event message
     * @param title
     * @param msg
     * @return
     */
    public boolean showConfirm() {
        return new ConfirmDialog(null, true).showDialog(title, msg);
    }

    /**
     * Shows a dialog with the event message
     * @param title
     * @param msg
     * @return
     */
    public boolean showYesNoConfirm() {
        return new YesNoDialog(null, true).showDialog(title, msg);
    }

   /**
    *
    * @param userName
    */
   public void writeLogin(String userName) {
        String timeStamp = TIMEDATEFMT.format(date);
        String fileStamp = DATEFMT.format(date);
        try {
            Writer fw = new Writer(new File(fileStamp + ".log"), true);
            fw.writeFile(timeStamp + ", " + userName + " logged in\n");
        } catch (IOException ex1) {
            new MessageDialog(null, true).showDialog("File error", "Could not write log file, terminating program...");
        }
    }

    private void writeLogFile() {
        String timeStamp = TIMEDATEFMT.format(date);
        String fileStamp = DATEFMT.format(date);
        try {
            Writer fw = new Writer(new File(fileStamp + ".log"), true);
            fw.writeFile(timeStamp + ", " + ex.getLocalizedMessage() + "\n");
        } catch (IOException ex1) {
            new MessageDialog(null, true).showDialog("File error", "Could not write log file, terminating program...");
        }
    }
}
